﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.IO;

namespace ProgettoWlanService
{
    public class ReadCfgFile {

        private FileStream fs;
        private StreamReader sr;

        private const string path = @"C:\Users\Alessandro Avila\Polito\Primo anno\Programmazione di sistema\C++\";

        private static string fileName;

        public ReadCfgFile(string fileName) {
            ReadCfgFile.fileName = fileName;
        }

        public void populate(ref List<Place> places) {

            if (File.Exists(path + fileName))
                fs = new FileStream(path + fileName, FileMode.Open);
            else
                return;
	        
            sr = new StreamReader(fs);

            places.Clear();

	        string line;
	        string[] words;

            while ((line = sr.ReadLine()) != null) {
                Place temp = new Place();
		        Dictionary<string, UInt32> networkList = new Dictionary<string, UInt32>();
		        List<string> actionList = new List<string>();
		        TimeSpan tspan = new TimeSpan();

                while (line != null && line.Length != 0) {
			        words = line.Split(':');
			        
                    switch (words[0]) {
				        case "p":
                            temp.setPlaceName(words[1]);
					        break;

				        case "n":
					        networkList.Add(words[1], Convert.ToUInt32(words[2]));
					        break;

				        case "a":
                            if(words[1] == "Firewall" || words[1] == "Networks")
                                temp.setActionList(words[1]);
                            else
                                temp.setActionList(line.Substring(2));
					        break;

				        case "d":
					        tspan = new TimeSpan(Convert.ToInt32(words[1]), Convert.ToInt32(words[2]), Convert.ToInt32(words[3]));
					        break;

			        }

			        line = sr.ReadLine();
		        }

                temp.setNetworkList(networkList);
		        temp.setTimeOfStay(tspan);
		        places.Add(temp);
            }

            sr.Close();
            fs.Close();
        }
    }
}
